﻿

namespace AvantGameMachine.Graphics.Animators
{
    public delegate bool UserAnimDelegate(RangeTimeAnimator _anim);

    public class RangeTimeAnimator : Animator
    {
        protected long startTime;
        protected long ellapsed;
        protected float delta;
        protected long duration;
        protected long endTime;

        public UserAnimDelegate UserAnim = null;

        public RangeTimeAnimator()
        {
        }

        public long StartTime
        {
            get { return startTime; }
            set { startTime = value; }
        }

        public long Ellapsed
        {
            get { return ellapsed; }
        }

        public float Delta
        {
            get { return delta; }
        }

        public long Duration
        {
            get { return duration; }
            set { duration = value; }
        }

        public long EndTime
        {
            get { return endTime; }
        }

        public override void  Start()
        {
            base.Start();
            startTime = AGMPlatform.TiManager.FrameCurrentTime;
            endTime = startTime + duration;
            delta = 0.0f;
        }

        public override bool OnAnimate()
        {
            bool result = base.OnAnimate();
            long a = AGMPlatform.TiManager.FrameCurrentTime;
            ellapsed = AGMPlatform.TiManager.FrameCurrentTime - startTime;
            if (ellapsed > 0)
                delta = (float)ellapsed / (float)duration;

            if (ellapsed >= duration)
                return false;

            if (UserAnim != null)
            {
                bool r2 = UserAnim(this);
                if (!r2)
                    return false;
            }

            return result;
        }
    }
}
